// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Collections.Generic;

namespace SnapObjects.Data
{
    internal class DataModelBinderFactory
    {
        public static readonly DataModelBinderFactory Current = new DataModelBinderFactory();

        private DataModelBinderFactory() { }

        public IDataModelBinder GetBinder(
            IList<DbColumnInfo> columnSchema, ITypeMapper typeMapper, Type modelType)
        {
            if (ModelHelper.IsDynamicModel(modelType))
            {
                throw new NotSupportedException("Unsupported model type.");
            }
            else
            {
                return new NoGenericDataModelBinder(modelType, columnSchema, typeMapper);
            }
        }

        public IDataModelBinder<TModel> GetBinder<TModel>(
            IList<DbColumnInfo> columnSchema, ITypeMapper typeMapper)
        {
            if (ModelHelper.IsDynamicModel(typeof(TModel)))
            {
                return new DynamicDataModelBinder(columnSchema, typeMapper) as DataModelBinder<TModel>;
            }
            else
            {
                return new PocoDataModelBinder<TModel>(columnSchema, typeMapper);
            }
        }
    }
}
